package cn.fengzhilun.javase.day16;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;

import javax.naming.spi.ResolveResult;

import org.junit.Test;

public class TestException {
	@Test
	public void test1() {
		//错误 栈内存不够
		List<String> list = new ArrayList<>();
		
		for(int i=0;i<10240000;i++) {
			String str = new String( new char[1024]);
			System.out.println(i++);
			list.add(str);
		}
	}
	
	@Test
	public void test() {
		StackOver();
	}
	public void StackOver() {
		
		StackOver();
	}
	
	@Test
	public void test2() throws IOException {
		Files.readAllLines(Paths.get(""));
	}
	
	@Test
	public void test3() {
		int [] array = new int[2];
		
		System.out.println(array[4]);
	}
	
	@Test
	public void test4() throws IOException {
		File f = new File("g:/");
		listFile(f);
		listFile1(f);
	}
	
	public void listFile(File f) {
		
		//遍历一个文件里的所有文件或者是目录
		File[] files = f.listFiles();
		
		//查看每个文件。看是否是文件夹
		for(File file : files) {
			if(file.isDirectory()) {
				listFile(file);
			}else {
				System.out.println(file);
			}
		}
	}
	
	public void listFile1(File f) throws IOException {
			Files.walkFileTree(f.toPath(), new FileVisitor<Path>() {

				@Override
				public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
					return FileVisitResult.CONTINUE;
					 
				}

				@Override
				public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
					System.out.println(file);
					System.out.println(attrs.size());
					return FileVisitResult.CONTINUE;
				}

				@Override
				public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
					return FileVisitResult.CONTINUE;
				}

				@Override
				public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
					return FileVisitResult.CONTINUE;
				}
			});
			
		}
}
